//
//  PublicDetailsVc.m
//  Courses
//
//  Created by xxx on 15/1/11.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "PublicDetailsVc.h"

#import "RelatedCourseCell.h"

#import "PublicCourseDetails.h"
#import "PublicCourse.h"

#import "UIImageView+WebCache.h"

#import "CoursedInstructorVc.h"
#import "SignInViewController.h"

//#import "webCastSDK.h"

@interface PublicDetailsVc() <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation PublicDetailsVc
{
    UITableView *_tableVRelatedCourses;
    
    UILabel *_courseNameLabel;
    
    UIImageView *_imgViewTeacherAvatar;
    UILabel *_lblTeacherName;
//    UILabel *_lbl
    
    PublicCourseDetails *_courseDetails;
}

static const CGFloat kVideoViewHeight = 0;
static const CGFloat kCourseStatusBarHeight = 25;
static const CGFloat kTableViewRelatedCoursesHeight = 150;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"课程";
    
//    [self addVideoView];
    [self addCourseStatusBar];
    [self addRelatedCoursesTableView];
    
    [self addCourseDescriptionBarView];
    [self addInstructorDetailView];
    
    [self loadData];
}

//- (void)addVideoView
//{
//    CGRect rect = AutoRectMake(0, 0, DEFAULT_WIDTH, kVideoViewHeight, WTFAutoLayoutTypePic);
//    UIView *videoBackgroundView = [[UIView alloc] initWithFrame:rect];
//    videoBackgroundView.backgroundColor = [UIColor blackColor];
//    
//    [self.view addSubview:videoBackgroundView];
//}


- (void)initGenseeVod
{
//    CGRect videoRect = AutoRectMake(0, 0, DEFAULT_WIDTH, kVideoViewHeight, WTFAutoLayoutTypePic);
//    CGRect docRect = CGRectMake(0, 0, 0, 0);
//    ReceiveCast *cast = [[ReceiveCast alloc] initWithGensee:self
//                                                 videoFrame:videoRect
//                                                   docFrame:docRect
//                                                     domain:@"yixue.gensee.com"
//                                                     number:@"07111173"
//                                                    nicName:nil
//                                              watchOrListen:YES
//                                                  watchWord:@""
//                                                serviceType:@"webcast"
//                                                   delegate:self];
}


- (void)addCourseStatusBar
{
    UIView *view = [[UIView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight, DEFAULT_WIDTH, kCourseStatusBarHeight, WTFAutoLayoutTypePic)];
    view.backgroundColor = [WTFColors defaultBarColor];
    
    _courseNameLabel = [[UILabel alloc] initWithFrame:AutoRectMake(20, 0, DEFAULT_WIDTH / 2, kCourseStatusBarHeight, WTFAutoLayoutTypePic)];
    _courseNameLabel.textColor = [UIColor whiteColor];
    [view addSubview:_courseNameLabel];
    
    UIButton *btnFav = [[UIButton alloc] initWithFrame:AutoRectMake(DEFAULT_WIDTH / 2 + 40, 0, 40, kCourseStatusBarHeight, WTFAutoLayoutTypePic)];
    [btnFav setTitle:@"收藏" forState:UIControlStateNormal];
    [btnFav addTarget:self action:@selector(fav) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnFav];
    
    UIButton *btnDownload = [[UIButton alloc] initWithFrame:AutoRectMake(DEFAULT_WIDTH / 2 + 40 * 2, 0, 40, kCourseStatusBarHeight, WTFAutoLayoutTypePic)];
    [btnDownload setTitle:@"下载" forState:UIControlStateNormal];
    [btnDownload addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnDownload];
    
    UIButton *btnShare = [[UIButton alloc] initWithFrame:AutoRectMake(DEFAULT_WIDTH / 2 + 40 * 3, 0, 40, kCourseStatusBarHeight, WTFAutoLayoutTypePic)];
    [btnShare setTitle:@"分享" forState:UIControlStateNormal];
    [btnShare addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnShare];
    
    [self.view addSubview:view];
}

- (void)addRelatedCoursesTableView
{
    _tableVRelatedCourses = [[UITableView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight + kCourseStatusBarHeight, DEFAULT_WIDTH, kTableViewRelatedCoursesHeight, WTFAutoLayoutTypePic) style:UITableViewStylePlain];
    _tableVRelatedCourses.dataSource = self;
    _tableVRelatedCourses.delegate = self;
    _tableVRelatedCourses.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:_tableVRelatedCourses];
}

- (void)addCourseDescriptionBarView
{
    UIView *courseDescBarView = [[UIView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight + kCourseStatusBarHeight + kTableViewRelatedCoursesHeight, DEFAULT_WIDTH, kCourseStatusBarHeight, WTFAutoLayoutTypePic)];
    courseDescBarView.backgroundColor = [WTFColors defaultBarColor];
    [self.view addSubview:courseDescBarView];
    
    UILabel *lblCourseDesc = [[UILabel alloc] initWithFrame:courseDescBarView.frame];
    lblCourseDesc.textAlignment = NSTextAlignmentCenter;
    lblCourseDesc.text = @"课程介绍";
    lblCourseDesc.textColor = [UIColor whiteColor];
    [self.view addSubview:lblCourseDesc];
}

- (void)addInstructorDetailView
{
    UIView *viewInstructor = [[UIView alloc] initWithFrame:AutoRectMake(0, kVideoViewHeight + kCourseStatusBarHeight * 2 + kTableViewRelatedCoursesHeight, DEFAULT_WIDTH, DEFAULT_TABLE_VIEW_HEIGHT - (kVideoViewHeight + kCourseStatusBarHeight * 2 + kTableViewRelatedCoursesHeight), WTFAutoLayoutTypePic)];
    [self.view addSubview:viewInstructor];
    
    _imgViewTeacherAvatar = [[UIImageView alloc] initWithFrame:AutoRectMake(20, 13, 80, 60, WTFAutoLayoutTypePic)];
    _imgViewTeacherAvatar.layer.cornerRadius = 18;
    _imgViewTeacherAvatar.layer.masksToBounds = YES;
    _imgViewTeacherAvatar.contentMode = UIViewContentModeScaleAspectFit;
    [viewInstructor addSubview:_imgViewTeacherAvatar];
    
    _lblTeacherName = [[UILabel alloc] initWithFrame:AutoRectMake(115, 10, 120, 30, WTFAutoLayoutTypeBar)];
    [viewInstructor addSubview:_lblTeacherName];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showInstructorDetailsVc)];
    [viewInstructor addGestureRecognizer:singleTap];
    // todo
    // lbl description
}

- (void)showInstructorDetailsVc
{
    NSLog(@"tapped on instructor view");
    
    if (_courseDetails)
    {
        CoursedInstructorVc *vc = [[CoursedInstructorVc alloc] init];
        Teacher *teacher = [[Teacher alloc] initWithCourseDetails:_courseDetails];
        vc.teacher = teacher;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - http requests

- (NSMutableArray *)items
{
    if (!_items) _items = [[NSMutableArray alloc] init];
    return _items;
}

- (void)loadData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        _courseDetails = [PublicCourseDetails courseDetailsByCourseId:_courseId];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.items = _courseDetails.relatedCourses;
            _courseNameLabel.text = _courseDetails.courseName;
            [_imgViewTeacherAvatar sd_setImageWithURL:[NSURL URLWithString:_courseDetails.teacherHeadPhotoUrl]];
            _lblTeacherName.text = [NSString stringWithFormat:@"讲师 : %@", _courseDetails.teacherName];
            [_tableVRelatedCourses reloadData];
        });
    });
}

#pragma mark - table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RelatedCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[RelatedCourseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (self.items.count > indexPath.row && [self.items[indexPath.row] isKindOfClass:[Course class]])
    {
        cell.index = indexPath.row;
        cell.course = self.items[indexPath.row];
    }
    return cell;
}

#pragma mark - table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kRelatedCourseCellHeight;
}

#pragma mark - private methods

- (void)fav
{
    NSLog(@"fav");
    if (![User isLoggedIn])
    {
        [SignInViewController popUpOnVc:self];
    }
    else
    {
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        BOOL result = [PublicCourse markPublicCourseById:_courseId];
//        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"result: %d", result);
//        });
//    });
    }
}

- (void)download
{
    NSLog(@"download");
}

- (void)share
{
    NSLog(@"share");
}

@end
